package common;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * 组合算法（回溯）
 *
 */
public class Combination {

    public static void main(String[] args) {
        List<List<Integer>> result = new ArrayList<>();
        List<Integer> elements = Arrays.asList(1, 2, 3);
        dfs(result, elements, 1, 0);
        System.out.println(result);
    }

    public static void dfs(List<List<Integer>> result, List<Integer> elements, int n, int first) {
        if (n == first) {
            result.add(new ArrayList<>(elements));
        }

        for (int i = first; i < elements.size(); i++) {
            Collections.swap(elements, i, first);
            dfs(result, elements, n, first + 1);
            Collections.swap(elements, i, first);
        }
    }
}
